* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file runs additional regressions for domestic market outcomes.

* Setup
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", clear

rename cvrnr cvrnrT
rename aar aarT
merge m:1 cvrnrT using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_did_jc_multi1865.dta", keep(match) nogen

*Lump all targets together
rename cvrnrT cvrnrT_ori
merge m:1 cvrnrA aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_did_jc_largestT_1865.dta", keepusing(cvrnrT) keep(match) nogen
collapse (sum) employee_cvrnr XVT_FIRM OMS_FIRM, by(cvrnrT aarT cvrnrA aar)

rename employee_cvrnr employee_cvrnrT
rename XVT_FIRM XVT_FIRMT
rename OMS_FIRM OMS_FIRMT

tempfile tempTfirm
save `tempTfirm.dta', replace

***

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", clear

rename cvrnr cvrnrA
rename aar aarA
rename employee_cvrnr employee_cvrnrA
rename XVT_FIRM XVT_FIRMA
rename OMS_FIRM OMS_FIRMA

keep cvrnrA aarA employee_cvrnrA XVT_FIRMA OMS_FIRMA

merge m:1 cvrnrA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_step2_maxdup2.dta", keep(match) nogen
expand maxdup2
bysort cvrnrA aarA: gen dup2 = _n
merge m:1 cvrnrA dup2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_step2_dup2.dta", keep(match) nogen
drop *dup*

gen d_yr = aarA-aar

drop if aar < 1996 | aar > 2015

merge m:1 cvrnrA aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_did_jc_largestT_1865.dta", keep(match) nogen

gen aarT = aarA
*Only keep if within the panel sample (i.e., will exclude where acquirer not in VARS and target in VARS)
merge m:1 cvrnrT aarT using `tempTfirm.dta', keep(match) nogen

gen lproductivityAT = ln((XVT_FIRMA + XVT_FIRMT)/(employee_cvrnrA + employee_cvrnrT))
gen lsalespwAT = ln((OMS_FIRMA + OMS_FIRMT)/(employee_cvrnrA + employee_cvrnrT))

tempfile tempATfirm
save `tempATfirm.dta', replace

********************************************************************************
* Main data	
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\regression_data_domestic.dta", replace

replace aar3 = aar3-1

rename aar aar_ori
rename aar3 aar

merge 1:1 cvrnrA aar aarA using `tempATfirm.dta', keepusing(lproductivityAT lsalespwAT employee_cvrnrT XVT_FIRMT OMS_FIRMT) keep(match master) 

gen lXVT_FIRM = ln(XVT_FIRM)
gen lOMS_FIRM = ln(OMS_FIRM)
gen lproductivity = ln(XVT_FIRM/employee_cvrnr)
gen lsalespw = ln(OMS_FIRM/employee_cvrnr)

rename lproductivityAT lproductivityAT_ori
rename lsalespwAT lsalespwAT_ori
gen lproductivityAT = lproductivityAT_ori if aarA<=aar & acquirer==1
replace lproductivityAT = lproductivity if aarA>aar & acquirer==1
gen lsalespwAT = lsalespwAT_ori if aarA<=aar & acquirer==1
replace lsalespwAT = lsalespw if aarA>aar & acquirer==1

replace lproductivityAT = lproductivity if acquirer==0
replace lsalespwAT = lsalespw if acquirer==0

* Calculate minimum number of employees in sample
egen minemployee_cvrnr_b4 = min(employee_cvrnr), by(cvrnrA)

* Figure B.4(b)	
foreach x in lproductivityAT lsalespwAT{
areg `x' p4 p3 p2 p1 test m1 m2 m3 m4 D_AA_1-D_AA_22 if minemployee_cvrnr_b4>=10, absorb(cvrnrA) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureB4.txt", keep(p4 p3 p2 p1 test m1 m2 m3 m4 D_AA_1-D_AA_22) bdec(4) sdec(4) nocons nonotes
}
